<template>
    <div class="randoms-com">
      <el-card class="box-card">
        <div class="search">
          <div class="search-left">
            <div class="one">
              <span>标签名称</span>
              <el-input
                v-model="FormData.tagName"
                placeholder="根据文章标题搜索"
              ></el-input>
            </div>
            <div class="one">
              <span style="margin-right: -27px">状态</span>
              <el-select v-model="FormData.state" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
            <div class="right">
              <el-button @click="clearinpt">清空</el-button>
              <el-button type="primary" @click="onsearch">搜索</el-button>
            </div>
          </div>
          <div class="add">
            <el-button type="success" icon="el-icon-edit" @click="addTags('add')">新增技巧</el-button>
          </div>
        </div>
        <el-alert
          :title="`数据一共${counts}条`"
          type="info"
          :closable="false"
          show-icon
        >
        </el-alert>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="130">
          </el-table-column>
          <el-table-column prop="tagName" label="标签名称" >
          </el-table-column>
          <el-table-column prop="username" label="创建者" >
          </el-table-column>

          <el-table-column prop="addDate" label="创建时间" width="220">
          </el-table-column>
          <el-table-column label="状态" width="100">
            <template #default="{ row }">
              <!-- 0禁用  1启用 -->
              <span>{{ row.state === 0 ? "已禁用" : "已启用" }}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="200">
            <template #default="{ row }">
              <el-button type="text" @click="stateArticles(row)">{{ row.state === 0 ? "开启" : "禁用" }}</el-button>
              <el-button type="text" :disabled="row.state === 1" @click="addTags('edit',row.id)">修改</el-button>
              <el-button type="text" :disabled="row.state === 1" @click="delTags(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="FormData.page"
          :page-sizes="[2, 3, 5, 6]"
          :page-size="FormData.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts"
        >
        </el-pagination>
      </el-card>
      <tags-add @addSuccess="addSuccess" :isEditOrAdd="isEditOrAdd" :editId="editId" v-if="isOpenTagsDialog" v-model="isOpenTagsDialog"></tags-add>
    </div>
  </template>

<script>
import { list, changeState, remove } from '../../api/hmmm/tags'
import tagsAdd from '../components/tags-add.vue'
export default {
  components: { tagsAdd },

  data () {
    return {
      isOpenTagsDialog: false, // 是否打开dialog
      counts: 0,
      tableData: [],
      FormData: {
        page: 1,
        pagesize: 5,
        tagName: '',
        state: null
      },
      options: [
        { value: 0, label: '已禁用' },
        { value: 1, label: '已启用' }
      ],
      editId: 0,
      state: '',
      isEditOrAdd: 'add'
    }
  },
  created () {
    this.getTagsList()
  },
  methods: {
    async delTags (id) {
      await remove({ id: id })
      this.$message.success('删除成功')
      this.getTagsList()
    },
    async stateArticles (row) {
      if (row.state === 1) {
        this.state = 0
      } else if (row.state === 0) {
        this.state = 1
      }
      await changeState({ id: row.id, state: this.state })
      this.$message.success('操作成功')
      this.getTagsList()
    },
    viewdetail (id) {
      this.viewDialog = true
      this.id = id
    },
    // 添加成功
    addSuccess () {
      this.getTagsList()
    },
    // 新增tags
    addTags (val, id) {
      this.isOpenTagsDialog = true
      this.isEditOrAdd = val
      if (val === 'edit') {
        this.editId = id
      }
    },
    clearinpt () {
      this.FormData.keyword = ''
      this.FormData.state = ''
    },
    onsearch () {
      this.getTagsList()
    },
    async getTagsList () {
      const res = await list(this.FormData)
      this.tableData = res.data.items
      this.counts = res.data.counts
    },
    handleSizeChange (val) {
      this.FormData.pagesize = val
      this.getTagsList()
      // console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.FormData.page = val
      this.getTagsList()
      // console.log(`当前页: ${val}`)
    }
  }
}
</script>
  <style>
  </style>
  <style scoped lang='scss'>
  .randoms-com {
    padding: 20px;
    .box-card {
      padding: 20px;
      .search {
        display: flex;
        justify-content: space-between;
        .search-left {
          display: flex;
          .one {
            display: flex;
            align-items: center;
            margin-right: 20px;
            span {
              width: 80px;
              font-size: 14px;
              color: #606266;
              font-weight: 700;
            }
          }
        }
      }
      ::v-deep .el-alert {
        margin: 15px 0;
      }
      .el-pagination {
        margin-top: 15px;
        text-align: right;
      }
    }
  }
  </style>
